package com.cxxx.guns.modular.portal.controller;

//
//                       _oo0oo_
//                      o8888888o
//                      88" . "88
//                      (| -_- |)
//                      0\  =  /0
//                    ___/`---'\___
//                  .' \\|     |// '.
//                 / \\|||  :  |||// \
//                / _||||| -:- |||||- \
//               |   | \\\  -  /// |   |
//               | \_|  ''\---/''  |_/ |
//               \  .-\__  '-'  ___/-. /
//             ___'. .'  /--.--\  `. .'___
//          ."" '<  `.___\_<|>_/___.' >' "".
//         | | :  `- \`.;`\ _ /`;.`/ - ` : | |
//         \  \ `_.   \_ __\ /__ _/   .-` /  /
//     =====`-.____`.___ \_____/___.-`___.-'=====
//                       `=---='
//
//
//     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//               佛祖保佑         永无BUG
//

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.cxxx.guns.modular.backend.common.Const;
import com.cxxx.guns.modular.backend.model.Announcement;
import com.cxxx.guns.modular.backend.model.Image;
import com.cxxx.guns.modular.backend.model.News;
import com.cxxx.guns.modular.backend.model.Quickimg;
import com.cxxx.guns.modular.backend.pojo.AnnouncementPojo;
import com.cxxx.guns.modular.backend.pojo.NewsPojo;
import com.cxxx.guns.modular.backend.pojo.Pages;
import com.cxxx.guns.modular.backend.service.IAnnouncementService;
import com.cxxx.guns.modular.backend.service.IImageService;
import com.cxxx.guns.modular.backend.service.INewsService;
import com.cxxx.guns.modular.backend.service.IQuickimgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * By CCZhang
 * Time: 2018/9/5:13:50
 */
@Controller
@RequestMapping("/portal")
public class IndexController {
    private static String PREFIX="/portal/";
    @Autowired
    private INewsService newsService;
    @Autowired
    private IAnnouncementService announcementService;
    @Autowired
    private IImageService imageService;
    @Autowired
    private IQuickimgService quickimgService;
    @RequestMapping("/index")
    public String index(Model model){
//       /*校园动态*/
        List<News> list1 = newsService.getListIndex(1);
        /*校园动态的最新的6个图片进行索引*/
        List<Quickimg> imgs1 = quickimgService.selectQuickByModel(1,6);
        model.addAttribute("list1",list1);
        model.addAttribute("imgs1",imgs1);

        /*通知公告*/
        List<News> list2 = newsService.getListIndex(2);
        model.addAttribute("list2",list2);
        /*家校互动*//*
        List<Announcement> list3 = announcementService.getListIndex(1);
        model.addAttribute("list3",list3);*/
        /*德育园地*/
        List<News> list3 = newsService.getListIndex(6);
        List<Quickimg> imgs3 = quickimgService.selectQuickByModel(6,1);
        model.addAttribute("imgs3",imgs3);
        model.addAttribute("list3",list3);
        /*教学之声，教学动态*/
        List<News> list4 = newsService.getListIndex(15);
        List<Quickimg> imgs4 = quickimgService.selectQuickByModel(15,1);
        model.addAttribute("list4",list4);
        model.addAttribute("imgs4",imgs4);
        /*教师频道*/
        List<News> list5 = newsService.getListIndex(23);
        List<Quickimg> imgs5 = quickimgService.selectQuickByModel(23,1);
        model.addAttribute("list5",list5);
        model.addAttribute("imgs5",imgs5);
        /*查询首页轮播图片内容给,根据时间来查询最新的几条记录*/
        List<Image> lunbo = imageService.imgageList(1,4);
        model.addAttribute("lunbo",lunbo);
        /*查询大美晨星中的内容*/
        List<Image> cx = imageService.imgageList(2,7);
        model.addAttribute("cx",cx);
        return PREFIX+"index.html";
    }
    /*点击查看详情的Controller,除家校互动的以外*/
    @RequestMapping(value = "details/{model}/{id}")
    public String Details( @PathVariable("id") String id,@PathVariable("model")int mod,Model model) {
        News news = newsService.selectById(id);
        //获取当前新闻的时间,通过时间来查询上一条下一条
        Date date = news.getCreateTime();
        News newsNext = newsService.selectNewsNext(mod,date);
        News newsPrevious = newsService.selectNewsPrevious(mod,date);
        NewsPojo newsPojo = new NewsPojo();
        List<com.cxxx.guns.modular.backend.model.Model> listModel = Const.getModelList();
        /**
         * 遍历整个list，取出对应的模型
         * **/
        for (int i=0;i<listModel.size();i++){
            if (listModel.get(i).getId() == mod){
                newsPojo.setModel(listModel.get(i).getName());
            }
        }
        newsPojo.setNews(news);
        if (newsNext!=null){
            newsPojo.setNextItemHead(newsNext.getNewsTitle());
            newsPojo.setNextItemId(newsNext.getId());
        }else{
            newsPojo.setNextItemHead("无");
            newsPojo.setNextItemId(news.getId());
        }
        if (newsPrevious!=null){
            newsPojo.setPreviousHead(newsPrevious.getNewsTitle());
            newsPojo.setPreviousId(newsPrevious.getId());
        }else {
            newsPojo.setPreviousHead("无");
            newsPojo.setPreviousId(news.getId());
        }
        List<News> list = newsService.getListIndex(news.getModel());
        model.addAttribute("news",newsPojo);
        model.addAttribute("list",list);
        return PREFIX +"starmsg.html";
    }
    //家校互动详情查看
    @RequestMapping(value = "/homeDetail/{id}")
    public String homeDetail(@PathVariable("id") int id, Model model){
        //根据id，查询当前家校互动信息
        Announcement announcement = announcementService.selectById(id);
        Date date = announcement.getCreateTime();
        Announcement next = announcementService.selectNext(date);
        Announcement previous = announcementService.selectPrevious(date);
        AnnouncementPojo announcementPojo = new AnnouncementPojo();
        announcementPojo.setAnnouncement(announcement);
        if (next!=null){
            announcementPojo.setNextItemHead(next.getNoticeTitle());
            announcementPojo.setNextItemId(next.getId());
        }else{
            announcementPojo.setNextItemHead("无");
            announcementPojo.setNextItemId(announcement.getId());
        }
        if (previous!=null){
            announcementPojo.setPreviousHead(previous.getNoticeTitle());
            announcementPojo.setPreviousId(previous.getId());
        }else{
            announcementPojo.setPreviousHead("无");
            announcementPojo.setPreviousId(announcement.getId());
        }
        List<Announcement> list = announcementService.getListIndex(1);
        model.addAttribute("news",announcementPojo);
        model.addAttribute("list",list);

        return PREFIX +"starmsgparents.html";
    }
    /*分页查询,根据类型和分页的页码，每页默认分15条记录*/
    @RequestMapping(value = "/pages/{mod}/{currentPage}")
    public String  getListMews(Model model, @PathVariable("mod") int mod, @PathVariable("currentPage") int currentPage, HttpServletRequest request){
        if(currentPage==0) {
            currentPage = Integer.parseInt(request.getParameter("pagesInformation"));
        }
        Page<News> page = new Page<>();
        Wrapper<News> wrapper = new EntityWrapper<>();
        wrapper.eq("model",mod);
        page.setCurrent(currentPage);
        page.setSize(15);
        Page<News> pagesInfo = newsService.selectPage(page,wrapper);
        /*自己封装的翻页对象*/
        Pages pages = new Pages();
        List<com.cxxx.guns.modular.backend.model.Model> ls = Const.getModelList();
        for(int i=0;i<ls.size();i++){
            if(ls.get(i).getId()==mod){
                pages.setModel(ls.get(i).getName());
            }
        }
        /*当前页*/
        pages.setCurrentPage(pagesInfo.getCurrent());
        /*总记录*/
        pages.setRecords(pagesInfo.getRecords());
        /*总页数*/
        pages.setTotal(pagesInfo.getPages());
        /*上一页页码*/
        int previousPage = pagesInfo.getCurrent()-1;
        if (previousPage==0){
            pages.setCurrentPagePrevious(1);
        }else{
            pages.setCurrentPagePrevious(previousPage);
        }
        /*下一页页码*/
        int nextPage = pagesInfo.getCurrent()+1;
        if(nextPage<=pagesInfo.getPages()){
            pages.setCurrentPageNext(nextPage);
        }else{
            pages.setCurrentPageNext((int)pagesInfo.getPages());
        }
        model.addAttribute("pagesInfo",pages);
        return PREFIX+"starlist.html";
    }
    /*家校互动版块,默认是15条*/
    @RequestMapping("/home/{currentPage}")
    public String home(@PathVariable("currentPage") int currentPage,Model model,HttpServletRequest request){
        if(currentPage==0){
            currentPage = Integer.parseInt(request.getParameter("pagesInformation"));
        }
        Page<Announcement> page = new Page();
        page.setSize(15);
        page.setCurrent(currentPage);
        Wrapper<Announcement> wrapper = new EntityWrapper<>();
        wrapper.eq("status",1);
        Page pagesInfo = announcementService.selectPage(page,wrapper);
        Pages pages = new Pages();
        pages.setCurrentPage(page.getCurrent());
        pages.setRecords(page.getRecords());
        pages.setTotal(page.getPages());
        /*上一页页码*/
        int previousPage = pagesInfo.getCurrent()-1;
        if (previousPage==0){
            pages.setCurrentPagePrevious(1);
        }else{
            pages.setCurrentPagePrevious(previousPage);
        }
        /*下一页页码*/
        int nextPage = pagesInfo.getCurrent()+1;
        if(nextPage<=pagesInfo.getPages()){
            pages.setCurrentPageNext(nextPage);
        }else{
            pages.setCurrentPageNext((int)pagesInfo.getPages());
        }
        model.addAttribute("pagesInfo",pages);

    return PREFIX + "starlistparents.html";
    }

}
